//
//  DropTableView.m
//  汽车大灯色温蓝牙
//
//  Created by dfwx on 2020/9/10.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "DropTableView.h"
#import "LearnView.h"
#import "PasswordView.h"
#import "DropListTableViewCell.h"

@interface DropTableView () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation DropTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIAlphaColorFromHex(0x222222, 0.1);
        
        self.nameArray = @[NSLocalizedString(@"UpdatePassword", @"密码修改"),NSLocalizedString(@"OperationExplain", @"操作说明"),NSLocalizedString(@"SoftUpdate", @"软件更新"),NSLocalizedString(@"SystemVersion", @"系统版本号")];
        
        UIButton *button = [[UIButton alloc]initWithFrame:self.bounds];
        button.backgroundColor = [UIColor clearColor];
        [button handleEven:UIControlEventTouchUpInside block:^(UIButton * _Nonnull sender) {
            [self removeFromSuperview];
        }];
        [self addSubview:button];
    }
    return self;
}

- (void)setLocationRect:(CGRect)locationRect {
    
    _locationRect = locationRect;
    
    self.contentView = [[UIView alloc]initWithFrame:CGRectMake(self.bounds.size.width-128-30, CGRectGetMaxY(locationRect)-15, 128, 185)];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.contentView];
    
    self.backImageView = [[UIImageView alloc]initWithFrame:self.contentView.bounds];
    self.backImageView.image = [UIImage imageNamed:@"eject_bg"];
    [self.contentView addSubview:self.backImageView];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(8, 12, 112, 160)];
    self.tableView.layer.cornerRadius = 10;
    self.tableView.layer.masksToBounds = YES;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DropListTableViewCell" bundle:nil] forCellReuseIdentifier:@"DropListTableViewCell"];
        
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.nameArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return tableView.bounds.size.height /4.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DropListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DropListTableViewCell" forIndexPath:indexPath];
    cell.contentLabel.text = self.nameArray[indexPath.row];
    cell.contentLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    cell.contentLabel.textColor = UIColorFromHex(0x666666);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 1) {
        LearnView *learnView = [[[NSBundle mainBundle]loadNibNamed:@"LearnView" owner:nil options:nil]lastObject];
        learnView.frame = [NSObject mainWindow].bounds;
        [[NSObject mainWindow] addSubview:learnView];
    }
    
    if (indexPath.row == 2) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(DropTableViewSoftUpdateClick)]) {
            [self.delegate DropTableViewSoftUpdateClick];
        }
    }
    
    if (indexPath.row == 3) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(DropTableViewSystemVersionClick)]) {
            [self.delegate DropTableViewSystemVersionClick];
        }
    }
    
    if (indexPath.row == 0) {
        
        UIButton *sender = [[UIButton alloc]initWithFrame:[NSObject mainWindow].bounds];
        sender.backgroundColor = UIAlphaColorFromHex(0x000000, 0.2);
        sender.tag = 9999;
        [sender handleEven:UIControlEventTouchUpInside block:^(UIButton * _Nonnull sender) {
            UIView *view = [[NSObject mainWindow]viewWithTag:8888];
            if (view) {
                [view removeFromSuperview];
            }
            [sender removeFromSuperview];
        }];
        [[NSObject mainWindow]addSubview:sender];
        
        PasswordView *pdView = [[PasswordView alloc] initWithFrame:CGRectMake(40, 200, ScreenWidth-80, 180)];
        pdView.title = WWNSLocalizedString(@"changePasswordTips", @"密码提示");
        pdView.tag = 8888;
        pdView.returnPasswordStringBlock = ^(NSString *password){
            // TODO:这里可以对输入的密码进行处理
            NSLog(@"%@",password);
            if (self.delegate && [self.delegate respondsToSelector:@selector(DropTableViewPasswordAction:)]) {
                [self.delegate DropTableViewPasswordAction:password];
            }
            
            UIView *view = [[NSObject mainWindow]viewWithTag:8888];
            UIView *view1 = [[NSObject mainWindow]viewWithTag:9999];
            if (view) {
                [view removeFromSuperview];
            }
            if (view1) {
                [view1 removeFromSuperview];
            }

            
        };
        [[NSObject mainWindow] addSubview:pdView];
    }
    
}

@end
